unit nView;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Grids, ComCtrls, ZConnection, ZAbstractTable, ZDataset, DB,
  ZAbstractRODataset, ZAbstractDataset, setting,fedit,frmtanda,TPS_TambahN;

type
  TPemilih = record
     ID:String;
     NIK:String;
     NKK:String;
     NAMA:String;
     TEMPAT:String;
     TANGGAL:String;
     IBU:String;
     AGAMA:String;
     KAWIN:String;
     KELAMIN:String;
     KELAINAN:String;
     ALAMAT:String;
     KOTA:String;
     KEC:String;
     DESA:String;
     RW:String;
     RT:String;
     DUSUN:String;
     KET:String;
  end;
  TTPemilih = array of TPemilih;
  TPenyimpan = record
     ID:String;
     NAMA:String;
  end;
  TTPenyimpan = array of TPenyimpan;
  TDpt = record
     KODE:String;
     NAMA:String;
  end;
  TTDpt = array of TDpt;
  TTabel = record
     ID:String;
     TABEL:String;
     COUNT:Integer;
  end;
  TTTabel = array of TTabel;
  TGanda = record
     ID1:String;
     ID2:String;
     KET:Integer;
  end;
  TTGanda = array of TGanda;
  TForm_NView = class(TForm)
    GroupBox3: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    cbKabkot: TComboBox;
    cbKec: TComboBox;
    cbDesa: TComboBox;
    btnRefresh: TButton;
    GroupBox1: TGroupBox;
    lblHal: TLabel;
    btnEdit: TButton;
    btnNext: TButton;
    btnPrev: TButton;
    btnTambah: TButton;
    sgUtama: TStringGrid;
    txtHal: TEdit;
    btnHapus: TButton;
    btnDaftar: TButton;
    Label4: TLabel;
    lblKet: TLabel;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    sgGanda: TStringGrid;
    PageControl2: TPageControl;
    TabSheet3: TTabSheet;
    TabSheet4: TTabSheet;
    TabSheet5: TTabSheet;
    TabSheet6: TTabSheet;
    ZQuery1: TZQuery;
    ZTable2: TZTable;
    ZConnection1: TZConnection;
    ZTable1: TZTable;
    ZQuery2: TZQuery;
    btnBuat: TButton;
    btnPrev2: TButton;
    txtHal2: TEdit;
    btnNext2: TButton;
    lblHal2: TLabel;
    btnKembalikan: TButton;
    sgSejarah: TStringGrid;
    lblKet2: TLabel;
    procedure btnHapusClick(Sender: TObject);
    procedure btnPrevClick(Sender: TObject);
    procedure btnNextClick(Sender: TObject);
    procedure txtHalKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure cbKabkotChange(Sender: TObject);
    procedure cbKecChange(Sender: TObject);
    procedure cbDesaChange(Sender: TObject);
    procedure btnRefreshClick(Sender: TObject);
    procedure sgUtamaClick(Sender: TObject);
    procedure btnDaftarClick(Sender: TObject);
    procedure btnTambahClick(Sender: TObject);
    procedure btnEditClick(Sender: TObject);
    procedure PageControl1Change(Sender: TObject);
    procedure PageControl2Change(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure btnBuatClick(Sender: TObject);
    procedure sgGandaClick(Sender: TObject);
    procedure btnKembalikanClick(Sender: TObject);
    procedure sgSejarahClick(Sender: TObject);
  private
    procedure FetchDB;
    procedure TampilHalaman(Hal:Integer = 1);
    procedure GetData(Hal: Integer);
    function getDesa(kode: String): String;
    function getIduser(id: String): String;
    function getTabel(id: String): String;
    function getIdsimpan(tabel: String): String;
    function getKabkot(kode: String): String;
    function getKec(kode: String): String;
    function getSimpan(id: String): String;
    procedure AutoSizeCol(Grid: TStringGrid; Column: integer);
    procedure getPage;
    function cekBGanda(id: String): Boolean;
    procedure showGanda(idganda: String);
    procedure FetchDB2(id: Integer);
    procedure TampilHalaman2(id:Integer; Hal: Integer = 1);
    procedure GetData2(id:Integer; Hal: Integer);
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form_NView: TForm_NView;
  fEdit:TForm3;
  fTanda:TForm5;
  tambahntps:TForm_TambahTPS;
  sKabkot:String;
  sKec:String;
  sDesa:String;
  cRow,crow2:Integer; //currentrow
  dRow,drow2:Integer; //data row
  cHal,chal2:Integer; //current halaman
  jHal,jhal2:Integer; //jumlah halaman
  bHal:Integer; //batas per halaman
  bPemilih,bPemilih2: TTPemilih;
  bPenyimpan:TTPenyimpan;
  bDesa:TTDpt;
  bKec:TTDpt;
  bKabkot:TTDpt;
  bTabel,bTabel2:TTTabel;
  bGanda:TTGanda;
  bIdganda:array of String;
  bGridganda:TTPenyimpan;
  ptable,ptable2:String;
  pid,pid2,tid,tid2:String;

implementation

{$R *.dfm}

procedure TForm_NView.btnBuatClick(Sender: TObject);
begin
  if (sdesa<>'') then begin
    tambahntps:=TForm_TambahTPS.Create(Application,sdesa);
  end;
end;

procedure TForm_NView.btnDaftarClick(Sender: TObject);
begin
  ShowMessage('Form daftarkan semua');
end;

procedure TForm_NView.btnEditClick(Sender: TObject);
begin
  tid:=getIdsimpan(ptable);
//  showmessage(ptable+'-'+pid+'-'+tid);
  fEdit := TForm3.Create(Application,ptable,pid,tid,false,1);
end;

procedure TForm_NView.btnHapusClick(Sender: TObject);
var
  pos:Integer;
  tots:Integer;
  ID:String;
  tabel:String;
  que:String;
begin
  {pos:=StringGrid1.Row;
  //getID
  tabel:=Copy(bPemilih[pos].ID,1,16);
  tots:=Length(bPemilih[pos].ID)-16;
  ID:=Copy(bPemilih[pos].ID,17,tots);
  //get tabel
  //get ID di tabel
  //query
  with ZQuery1 do
    begin
      Active := False;
      que:='DELETE FROM '+tabel+' WHERE ID_USER='+ID;
      SQL.Text := que;
      ExecSQL;
    end;
  //hapus array
  ShowMessage('Pemilih telah dihapus');}
  //hati-hati ganti
  tid:=getIdsimpan(ptable);
  if (cekBGanda(tid+'-'+pid)) then
    fTanda := TForm5.Create(Application,ptable,pid,true,tid,1)
  else
    fTanda := TForm5.Create(Application,ptable,pid,false,tid,1)
end;

procedure TForm_NView.btnKembalikanClick(Sender: TObject);
var
  query,ique,itabel:String;
  buttonSelected : Integer;
begin
  ShowMessage(ptable2+' '+pid2+' '+tid2);
  buttonSelected := MessageDlg('Apakah anda yakin akan mengembalikan data ini?',mtConfirmation , mbOKCancel, 0);
  if buttonSelected = mrOK  then begin
    with ZQuery1 do begin
      active:=false;
      sql.Clear;
      query:='SELECT * FROM '+ptable2+' WHERE ID_USER='+pid2;
  //      ShowMessage(query);
      sql.Text:=query;
      Active:=true;
      first;
      iQue:='INSERT INTO `history` (ID_USER, `NIK`, `NKK`, `NAMA`, `TEMPAT`, `TANGGAL`, '+
      '`IBU`, `AGAMA`, `KAWIN`, `KELAMIN`, `KELAINAN`, `ALAMAT`, `RW`, `RT`, `DUSUN`, `KET`, '+
      '`ID_TANGGAL`, TPS, STATUS1, STATUS2, GANDA, STATUS3, STATUSP, ALASANP'+
      ',CACAT,PENDIDIKAN,PEKERJAAN,STATUSR,WN) VALUES '+
      '('+QuotedStr(tid2+'-'+FieldByName('ID_USER').AsString)+
      ','+QuotedStr(FieldByName('NIK').AsString)+
      ','+QuotedStr(FieldByName('NKK').AsString)+
      ','+QuotedStr(FieldByName('NAMA').AsString)+
      ','+QuotedStr(FieldByName('TEMPAT').AsString)+
      ','+QuotedStr(FieldByName('TANGGAL').AsString)+
      ','+QuotedStr(FieldByName('IBU').AsString)+
      ','+QuotedStr(FieldByName('AGAMA').AsString)+
      ','+QuotedStr(FieldByName('KAWIN').AsString)+
      ','+QuotedStr(FieldByName('KELAMIN').AsString)+
      ','+QuotedStr(FieldByName('KELAINAN').AsString)+
      ','+QuotedStr(FieldByName('ALAMAT').AsString)+
      ','+QuotedStr(FieldByName('RW').AsString)+
      ','+QuotedStr(FieldByName('RT').AsString)+
      ','+QuotedStr(FieldByName('DUSUN').AsString)+
      ','+QuotedStr(FieldByName('KET').AsString)+
      ','+FieldByName('ID_TANGGAL').AsString+
      ','+FieldByName('TPS').AsString+
      ','+QuotedStr(FieldByName('STATUS1').AsString)+
      ','+QuotedStr(FieldByName('STATUS2').AsString)+
      ','+QuotedStr(FieldByName('GANDA').AsString)+
      ','+QuotedStr(FieldByName('STATUS3').AsString)+
      ','+QuotedStr(FieldByName('STATUSP').AsString)+
      ','+QuotedStr(FieldByName('ALASANP').AsString)+
      ','+QuotedStr(FieldByName('CACAT').AsString)+
      ','+QuotedStr(FieldByName('PENDIDIKAN').AsString)+
      ','+QuotedStr(FieldByName('PEKERJAAN').AsString)+
      ','+QuotedStr(FieldByName('STATUSR').AsString)+
      ','+QuotedStr(FieldByName('WN').AsString)+
      ')';
      //
      active:=false;
      sql.Clear;
  //      ShowMessage(ique);
      sql.Text:=ique;
      ExecSQL;
      //
      active:=false;
      sql.Clear;
      itabel:=ptable2+'-'+pid2;
      query:='UPDATE '+ptable2+' SET STATUS2="0" WHERE ID_USER='+pid2;
  //    ShowMessage(query);
      sql.Text:=query;
      ExecSQL;
    end;
    ShowMessage('Data telah dikembalikan');
  end;
end;

procedure TForm_NView.btnNextClick(Sender: TObject);
begin
  TampilHalaman(cHal+1);
  txtHal.Text:=IntToStr(cHal);
end;

procedure TForm_NView.btnPrevClick(Sender: TObject);
begin
  TampilHalaman(cHal-1);
  txtHal.Text:=IntToStr(cHal);
end;

procedure TForm_NView.btnRefreshClick(Sender: TObject);
begin
  FetchDB;
  btnTambah.Enabled:=true;
  btnEdit.Enabled:=False;
  btnDaftar.Enabled:=true;
  btnHapus.Enabled:=False;
  btnRefresh.Enabled:=true;
  PageControl1.ActivePage:=TabSheet1;
end;

procedure TForm_NView.FetchDB();
var
  kode:String;
  query:String;
  kKota:String;
  kKec:String;
  kDesa:String;
  tblName:String;
  i:Integer;
  count,count2:Integer;
  gdata:Boolean;
  que:String;
begin
  lblHal.Caption:='-';
  gdata:=False;
  //
  for i := 1 to sgUtama.RowCount - 1
  do sgUtama.Rows[i].Clear;
  sgUtama.RowCount:=2;
  for i := 1 to sgGanda.RowCount - 1
  do sgGanda.Rows[i].Clear;
  sgGanda.RowCount:=2;
  for i := 1 to sgSejarah.RowCount - 1
  do sgSejarah.Rows[i].Clear;
  sgSejarah.RowCount:=2;
  //
  if (sDesa<>'') then
    begin
      kode:=sDesa;
    end
  else if (sKec<>'') then
    begin
      kode:=sKec;
    end
  else if (sKabkot<>'') then
    begin
      kode:=sKabkot;
    end
  else
    begin
      kode:='';
    end;
  if (kode='') then
    query:='1'
  else
    query:='NAMA LIKE "pemilih_'+kode+'%"';
  ZQuery1.Active := False;
  query:= 'SELECT * FROM penyimpan WHERE '+query;
  ZQuery1.SQL.Text := query;
  ZQuery1.Active := True;
  ZQuery1.First;  //dapat tabel yang di simpan
  SetLength(bTabel,ZQuery1.RecordCount);
  i:=0;
  count:=0;
  count2:=0;
  while (not ZQuery1.Eof) do
    begin
      gdata:=True;
      ZQuery2.Active := False;
      tblName:=ZQuery1.FieldByName('NAMA').AsString;
      ZQuery2.SQL.Clear;
      que:='SELECT COUNT(*) FROM '+tblname;
      que:=que+' WHERE STATUS2="0" OR STATUS2="4"';
//      showmessage(que);
      ZQuery2.SQL.Text:=que;
      ZQuery2.Active := True;
      ZQuery2.First;  //tabel
      count:=count+ZQuery2.Fields[0].AsInteger;
      //ShowMessage(IntToStr(count));
      if (ZQuery2.Fields[0].AsInteger>0) then begin
        bTabel[i].ID:=ZQuery1.FieldByName('ID').AsString;
        bTabel[i].TABEL:=ZQuery1.FieldByName('NAMA').AsString;
        bTabel[i].COUNT:=count;
        Inc(i);
      end
        else
        SetLength(bTabel,length(btabel)-1);
      //GANDA COUNT
      ZQuery2.SQL.Clear;
      que:='SELECT COUNT(*) FROM data_ganda';
      que:=que+' WHERE ID_USER1 LIKE "'+getIdsimpan(tblname)+'-%" OR ID_USER2 LIKE "'+getIdsimpan(tblname)+'-%"';
//      showmessage(que);
      ZQuery2.SQL.Text:=que;
      ZQuery2.Active := True;
      ZQuery2.First;  //tabel
      count2:=count2+ZQuery2.Fields[0].AsInteger;
      //
      ZQuery1.Next;
    end;
    jHal:=count div bHal;
    if ((count mod bHal)>0) then jHal:=jHal+1;
    if (gdata and (count>0)) then TampilHalaman(1);
    //text ket
    lblKet.Caption:=IntToStr(count)+' orang, '+IntToStr(count2)+' dicurigai ganda';
end;

procedure TForm_NView.TampilHalaman(Hal:Integer = 1);
var
  iter,i:Integer;
  tabel,iduser,tanda,query:String;
begin
  txtHal.Enabled:=True;
  lblHal.Caption := IntToStr(Hal)+' of '+IntToStr(jHal);
  cHal:=Hal;
  if (cHal=1) then
    btnPrev.Enabled:=False
  else
    btnPrev.Enabled:=True;
  if (cHal=jHal) then
    btnNext.Enabled:=False
  else
    btnNext.Enabled:=True;
  //get to bpemilih
  GetData(Hal);
  cRow:=1;
  SetLength(bGanda,0);
  while (cRow<=bHal) and (crow<=High(bpemilih)) do
   begin
    sgUtama.RowCount:=cRow+1;
    tabel:=getTabel(bPemilih[cRow].ID);
    tabel:=getIdsimpan(tabel);
    iduser:=getIduser(bPemilih[cRow].ID);
    tanda:='';
    with ZQuery1 do begin
      Active:=false;
      sql.Clear;
      query:='SELECT * FROM data_ganda WHERE ID_USER1="'+tabel+'-'+iduser+'"';
      sql.Text:=query;
      Active:=true;
      First;
      if (FieldByName('ID_USER1').AsString<>'') then begin
        tanda:='*';
        while (not Eof) do begin
          SetLength(bGanda,(Length(bGanda)+1));
          bGanda[Length(bGanda)-1].ID1:=FieldByName('ID_USER1').AsString;
          bGanda[Length(bGanda)-1].ID2:=FieldByName('ID_USER2').AsString;
          bGanda[Length(bGanda)-1].KET:=FieldByName('KETERANGAN').AsInteger;
          Next;
        end;
      end;
      //
      Active:=false;
      sql.Clear;
      query:='SELECT * FROM data_ganda WHERE ID_USER2="'+tabel+'-'+iduser+'"';
      sql.Text:=query;
      Active:=true;
      First;
      if (FieldByName('ID_USER2').AsString<>'') then begin
        tanda:='*';
        while (not Eof) do begin
          SetLength(bGanda,(Length(bGanda)+1));
          bGanda[Length(bGanda)-1].ID1:=FieldByName('ID_USER2').AsString;
          bGanda[Length(bGanda)-1].ID2:=FieldByName('ID_USER1').AsString;
          bGanda[Length(bGanda)-1].KET:=FieldByName('KETERANGAN').AsInteger;
          Next;
        end;
      end;
    end;
    sgUtama.Cells[0,cRow]:=IntToStr((cHal-1)*bHal+cRow)+tanda;
    sgUtama.Cells[1,cRow]:=bPemilih[cRow].NIK;
    sgUtama.Cells[2,cRow]:=bPemilih[cRow].NKK;
    sgUtama.Cells[3,cRow]:=bPemilih[cRow].NAMA;
    sgUtama.Cells[4,cRow]:=bPemilih[cRow].TEMPAT;
    sgUtama.Cells[5,cRow]:=bPemilih[cRow].TANGGAL;
    sgUtama.Cells[6,cRow]:=bPemilih[cRow].IBU;
//    sgUtama.Cells[7,cRow]:=bPemilih[cRow].AGAMA;
    sgUtama.Cells[8,cRow]:=bPemilih[cRow].KAWIN;
    sgUtama.Cells[9,cRow]:=bPemilih[cRow].KELAMIN;
//    sgUtama.Cells[10,cRow]:=bPemilih[cRow].KELAINAN;
    sgUtama.Cells[11,cRow]:=bPemilih[cRow].ALAMAT;
    sgUtama.Cells[12,cRow]:=bPemilih[cRow].KOTA;
    sgUtama.Cells[13,cRow]:=bPemilih[cRow].KEC;
    sgUtama.Cells[14,cRow]:=bPemilih[cRow].DESA;
    sgUtama.Cells[15,cRow]:=bPemilih[cRow].RW;
    sgUtama.Cells[16,cRow]:=bPemilih[cRow].RT;
    sgUtama.Cells[17,cRow]:=bPemilih[cRow].DUSUN;
    sgUtama.Cells[18,cRow]:=bPemilih[cRow].KET;
    if (tanda='') then
      sgUtama.Cells[19,cRow]:='TIDAK'
    else
      sgUtama.Cells[19,cRow]:='YA';
    Inc(crow);
   end;
   //autosize
   for i := 0 to sgUtama.ColCount - 1 do
    AutoSizeCol(sgUtama, i);
end;

procedure TForm_NView.GetData(Hal:Integer);
var
  query:String;
  kKota:String;
  kKec:String;
  kDesa:String;
  tabel:TTabel;
  tblName,idsimpan,iduser,idganda:String;
  i:Integer;
  vRow,ujung,awal:Integer;
begin
  dRow:=1;
  vRow:=1;
  //cek
  ujung:=Hal*bHal;
  i:=0;
  if (jHal>1) then begin
    for I := 0 to (Length(bTabel)) - 1 do begin
      if (ujung<=bTabel[i].COUNT) then
        break;
    end;
    if (i>0) then begin
      ujung:=ujung-btabel[i-1].COUNT;
      if (ujung<bHal) then begin
        Dec(i);
        awal:=bTabel[i].COUNT-(bHal-ujung);
      end else
        awal:=ujung-bHal;
    end
    else begin
      awal:=ujung-bHal;
    end;
  end
    else
  begin
    awal:=0;
    ujung:=bhal;
  end;
  //showmessage(IntToStr(length(btabel)));
  tblName:=btabel[i].TABEL;
  idsimpan:=btabel[i].ID;
  ZQuery1.Active := False;
  query:= 'SELECT * FROM '+tblName;
  query:=query+' WHERE STATUS2="0" OR STATUS2="4"';
  query:=query+' ORDER BY NKK ASC LIMIT '+IntToStr(awal)+','+IntToStr(bhal);
  ZQuery1.SQL.Text := query;
  ZQuery1.Active := True;
  ZQuery1.First;  //tabel simpan
  kKota:=Copy(tblName,9,2);
  kKota:=getKabkot(kKota);
  kKec:=Copy(tblName,9,5);
  kKec:=getKec(kKec);
  kDesa:=Copy(tblName,9,8);
  kDesa:=getDesa(kDesa);
  dRow:=dRow+ZQuery1.RecordCount;
  SetLength(bPemilih,dRow);
  while (not ZQuery1.Eof) do begin
    bPemilih[vRow].ID :=tblName+ZQuery1.Fields[0].AsString;
    bPemilih[vRow].NIK:=ZQuery1.Fields[1].AsString;
    bPemilih[vRow].NKK:=ZQuery1.Fields[2].AsString;
    bPemilih[vRow].NAMA:=ZQuery1.Fields[3].AsString;
    bPemilih[vRow].TEMPAT:=ZQuery1.Fields[4].AsString;
    bPemilih[vRow].TANGGAL:=ZQuery1.Fields[5].AsString;
    bPemilih[vRow].IBU:=ZQuery1.Fields[6].AsString;
    bPemilih[vRow].AGAMA:=ZQuery1.Fields[7].AsString;
    bPemilih[vRow].KAWIN:=ZQuery1.Fields[8].AsString;
    bPemilih[vRow].KELAMIN:=ZQuery1.Fields[9].AsString;
    bPemilih[vRow].KELAINAN:=ZQuery1.Fields[10].AsString;
    bPemilih[vRow].ALAMAT:=ZQuery1.Fields[11].AsString;
    bPemilih[vRow].KOTA:=kKota;
    bPemilih[vRow].KEC:=kKec;
    bPemilih[vRow].DESA:=kDesa;
    bPemilih[vRow].RW:=ZQuery1.Fields[12].AsString;
    bPemilih[vRow].RT:=ZQuery1.Fields[13].AsString;
    bPemilih[vRow].DUSUN:=ZQuery1.Fields[14].AsString;
    bPemilih[vRow].KET:=ZQuery1.Fields[15].AsString;
    Inc(vrow);
    ZQuery1.Next;
  end;
  if ((ujung<bhal) and (jhal>1)) then begin
    inc(i);
    tblName:=btabel[i].TABEL;
    idsimpan:=btabel[i].ID;
    ZQuery1.Active := False;
    query:= 'SELECT * FROM '+tblName;
    query:=query+' WHERE STATUS2="0" OR STATUS2="4"';
    query:=query+' ORDER BY NKK ASC LIMIT 0,'+IntToStr(ujung);
    //ShowMessage(query);
    ZQuery1.SQL.Text := query;
    ZQuery1.Active := True;
    ZQuery1.First;  //tabel simpan
    kKota:=Copy(tblName,9,2);
    kKota:=getKabkot(kKota);
    kKec:=Copy(tblName,9,5);
    kKec:=getKec(kKec);
    kDesa:=Copy(tblName,9,8);
    kDesa:=getDesa(kDesa);
    dRow:=dRow+ZQuery1.RecordCount;
    SetLength(bPemilih,dRow);
    while (not ZQuery1.Eof) do begin
      bPemilih[vRow].ID :=tblName+ZQuery1.Fields[0].AsString;
      bPemilih[vRow].NIK:=ZQuery1.Fields[1].AsString;
      bPemilih[vRow].NKK:=ZQuery1.Fields[2].AsString;
      bPemilih[vRow].NAMA:=ZQuery1.Fields[3].AsString;
      bPemilih[vRow].TEMPAT:=ZQuery1.Fields[4].AsString;
      bPemilih[vRow].TANGGAL:=ZQuery1.Fields[5].AsString;
      bPemilih[vRow].IBU:=ZQuery1.Fields[6].AsString;
      bPemilih[vRow].AGAMA:=ZQuery1.Fields[7].AsString;
      bPemilih[vRow].KAWIN:=ZQuery1.Fields[8].AsString;
      bPemilih[vRow].KELAMIN:=ZQuery1.Fields[9].AsString;
      bPemilih[vRow].KELAINAN:=ZQuery1.Fields[10].AsString;
      bPemilih[vRow].ALAMAT:=ZQuery1.Fields[11].AsString;
      bPemilih[vRow].KOTA:=kKota;
      bPemilih[vRow].KEC:=kKec;
      bPemilih[vRow].DESA:=kDesa;
      bPemilih[vRow].RW:=ZQuery1.Fields[12].AsString;
      bPemilih[vRow].RT:=ZQuery1.Fields[13].AsString;
      bPemilih[vRow].DUSUN:=ZQuery1.Fields[14].AsString;
      bPemilih[vRow].KET:=ZQuery1.Fields[15].AsString;
      Inc(vrow);
      ZQuery1.Next;
    end;
  end;
end;

procedure TForm_NView.AutoSizeCol(Grid: TStringGrid; Column: integer);
var
  i, W, WMax: integer;
begin
  WMax := 0;
  for i := 0 to (Grid.RowCount - 1) do begin
    W := Grid.Canvas.TextWidth(Grid.Cells[Column, i]);
    if W > WMax then
      WMax := W;
  end;
  Grid.ColWidths[Column] := WMax + 5;
end;

function TForm_NView.getKec(kode:String):String;
var
  I:integer;
begin
  for I := 0 to Length(bKec) - 1 do begin
    if (bKec[i].KODE=kode) then
      break;
  end;
    Result:=bKec[i].NAMA;
end;

function TForm_NView.getKabkot(kode:String):String;
var
  I:integer;
begin
  for I := 0 to Length(bKabkot) - 1 do begin
    if (bKabkot[i].KODE=kode) then
      break;
  end;
    Result:=bKabkot[i].NAMA;
end;

function TForm_NView.getIdsimpan(tabel:String):String;
var
  I:integer;
  res:String;
begin
  res:='';
  for I := 0 to Length(bPenyimpan) - 1 do begin
    if (bPenyimpan[i].NAMA=tabel) then begin
      res:=bPenyimpan[i].ID;
      break;
    end;
  end;
    Result:=res;
end;

function TForm_NView.getSimpan(id:String):String;
var
  I:integer;
  res:String;
begin
  res:='';
  for I := 0 to Length(bPenyimpan) - 1 do begin
    if (bPenyimpan[i].ID=id) then begin
      res:=bPenyimpan[i].NAMA;
      break;
    end;
  end;
    Result:=res;
end;

function TForm_NView.getTabel(id:String):String;
begin
  Result:=Copy(id,1,16);
end;

function TForm_NView.getIduser(id:String):String;
begin
  Result:=Copy(id,17,(Length(id)-16));
end;

function TForm_NView.getDesa(kode:String):String;
var
  I:integer;
begin
  for I := 0 to Length(bDesa) - 1 do begin
    if (bDesa[i].KODE=kode) then
      break;
  end;
    Result:=bDesa[i].NAMA;
end;

procedure TForm_NView.btnTambahClick(Sender: TObject);
begin
  showmessage('Form tambah');
end;

procedure TForm_NView.cbKabkotChange(Sender: TObject);
var
  pilihan:String;
begin
  if (cbKabkot.ItemIndex > 0) then
  begin
    pilihan := cbKabkot.Items[cbKabkot.ItemIndex];
    with ZTable1 do
      begin
        Active := False;
        TableName := 'kabkot';
        Filter := 'TAMPIL = '+QuotedStr(pilihan);
        Filtered := True;
        Active := True;
        First;
        sKabkot := FieldByName('KODE').AsString;
        //cb2
        Active := False;
        TableName := 'kecamatan';
        Filter := 'KODE_KABKOT = '+QuotedStr(sKabkot);
        Filtered := True;
        Active := True;
        First;
      end;
    cbkec.Clear;
    cbDesa.Clear;
    cbkec.Items.Add('SEMUA KECAMATAN');
    while (not ZTable1.Eof) do
      begin
        cbkec.Items.Add(ZTable1.FieldByName('TAMPIL').AsString);
        ZTable1.Next;
      end;
  end
    else
  begin
    sKabkot:='';
    cbkec.Clear;
    cbDesa.Clear;
  end;
  sKec:='';
  sDesa:='';
  btnTambah.Enabled:=False;
  btnEdit.Enabled:=False;
  btnDaftar.Enabled:=false;
  btnHapus.Enabled:=False;
  btnBuat.Enabled:=false;
  btnRefresh.Enabled:=true;
end;

procedure TForm_NView.cbKecChange(Sender: TObject);
var
  pilihan:String;
begin
  if (cbKec.ItemIndex > 0) then
  begin
    pilihan := cbKec.Items[cbKec.ItemIndex];
    ZTable1.Active := False;
    ZTable1.TableName := 'kecamatan';
    ZTable1.Filter := 'TAMPIL = '+QuotedStr(pilihan);
    ZTable1.Filtered := True;
    ZTable1.Active := True;
    ZTable1.First;
    sKec := ZTable1.FieldByName('KODE_KECAMATAN').AsString;
    //cb3
    ZTable1.Active := False;
    ZTable1.TableName := 'desa';
    ZTable1.Filter := 'KODE_KECAMATAN = '+QuotedStr(sKec);
    ZTable1.Filtered := True;
    ZTable1.Active := True;
    ZTable1.First;
    cbdesa.Clear;
    cbdesa.Items.Add('SEMUA DESA');
    while (not ZTable1.Eof) do
      begin
        cbdesa.Items.Add(ZTable1.FieldByName('TAMPIL').AsString);
        ZTable1.Next;
      end;
  end
    else
  begin
    sKec:='';
    cbdesa.Clear;
  end;
  sDesa:='';
  btnTambah.Enabled:=False;
  btnEdit.Enabled:=False;
  btnDaftar.Enabled:=false;
  btnHapus.Enabled:=False;
  btnBuat.Enabled:=false;
  btnRefresh.Enabled:=true;
end;

procedure TForm_NView.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  SetLength(bPemilih,0);
  SetLength(bPemilih2,0);
  SetLength(bTabel,0);
  SetLength(bTabel2,0);
  SetLength(bGanda,0);
  SetLength(bIdganda,0);
  SetLength(bGridganda,0);
  Action := caFree;
end;

procedure TForm_NView.FormCreate(Sender: TObject);
var
  I: Integer;
  setting:TSetting_Form;
begin
  setting:=TSetting_Form.Create;
  ZConnection1.HostName:=setting.host;
  ZConnection1.Protocol:=setting.protocol;
  ZConnection1.Database:=setting.database;
  ZConnection1.User:=setting.user;
  ZConnection1.Password:=setting.password;
  ZConnection1.Connected:=true;
  ztable1.Connection:=ZConnection1;
  ZQuery1.Connection:=ZConnection1;
  ztable2.Connection:=ZConnection1;
  ZQuery2.Connection:=ZConnection1;
  ZConnection1.LoginPrompt:=False;
  ZConnection1.Connected := True;
  //Komponen
  sKabkot:='';
  sKec:='';
  sDesa:='';
  cRow:=2;  //wadah row
  dRow:=1;  //menyimpan banyaknya data
  bHal:=25;  //pemilih per halaman
  lblHal.Caption:= '-';
  btnRefresh.Enabled:=False;
  txtHal.Enabled:=False;
  btnPrev.Enabled:=False;
  btnNext.Enabled:=False;
  txtHal2.Enabled:=false;
  btnNext2.Enabled:=false;
  btnPrev2.Enabled:=false;
  btnTambah.Enabled:=False;
  btnEdit.Enabled:=False;
  btnHapus.Enabled:=False;
  btnBuat.Enabled:=false;
  btnDaftar.Enabled:=False;
  lblKet.Caption:='';
  lblKet2.Caption:='';
  //Grid
  sgUtama.ColCount:=20;
  sgUtama.Cells[0,0]:='NO';
  sgUtama.Cells[1,0]:='NIK';
  sgUtama.Cells[2,0]:='NKK';
  sgUtama.Cells[3,0]:='Nama';
  sgUtama.Cells[4,0]:='Tempat Lahir';
  sgUtama.Cells[5,0]:='Tanggal Lahir';
  sgUtama.Cells[6,0]:='Nama Ibu';
  sgUtama.Cells[7,0]:='';
  sgUtama.Cells[8,0]:='Perkawinan';
  sgUtama.Cells[9,0]:='Jenis Kelamin';
  sgUtama.Cells[10,0]:='';
  sgUtama.Cells[11,0]:='Alamat';
  sgUtama.Cells[12,0]:='Kota';
  sgUtama.Cells[13,0]:='Kecamatan';
  sgUtama.Cells[14,0]:='Desa';
  sgUtama.Cells[15,0]:='RW';
  sgUtama.Cells[16,0]:='RT';
  sgUtama.Cells[17,0]:='Dusun';
  sgUtama.Cells[18,0]:='Ket';
  sgUtama.Cells[19,0]:='Ganda';
  sgUtama.RowCount:=cRow;
  //
  sgGanda.ColCount:=19;
  sgGanda.Cells[0,0]:='NO';
  sgGanda.Cells[1,0]:='NIK';
  sgGanda.Cells[2,0]:='NKK';
  sgGanda.Cells[3,0]:='Nama';
  sgGanda.Cells[4,0]:='Tempat Lahir';
  sgGanda.Cells[5,0]:='Tanggal Lahir';
  sgGanda.Cells[6,0]:='Nama Ibu';
  sgGanda.Cells[7,0]:='';
  sgGanda.Cells[8,0]:='Perkawinan';
  sgGanda.Cells[9,0]:='Jenis Kelamin';
  sgGanda.Cells[10,0]:='';
  sgGanda.Cells[11,0]:='Alamat';
  sgGanda.Cells[12,0]:='Kota';
  sgGanda.Cells[13,0]:='Kecamatan';
  sgGanda.Cells[14,0]:='Desa';
  sgGanda.Cells[15,0]:='RW';
  sgGanda.Cells[16,0]:='RT';
  sgGanda.Cells[17,0]:='Dusun';
  sgGanda.Cells[18,0]:='Ket';
  sgGanda.RowCount:=cRow;
  //
  sgSejarah.ColCount:=20;
  sgSejarah.Cells[0,0]:='NO';
  sgSejarah.Cells[1,0]:='NIK';
  sgSejarah.Cells[2,0]:='NKK';
  sgSejarah.Cells[3,0]:='Nama';
  sgSejarah.Cells[4,0]:='Tempat Lahir';
  sgSejarah.Cells[5,0]:='Tanggal Lahir';
  sgSejarah.Cells[6,0]:='Nama Ibu';
  sgSejarah.Cells[7,0]:='Agama';
  sgSejarah.Cells[8,0]:='Perkawinan';
  sgSejarah.Cells[9,0]:='Jenis Kelamin';
  sgSejarah.Cells[10,0]:='Kelainan';
  sgSejarah.Cells[11,0]:='Alamat';
  sgSejarah.Cells[12,0]:='Kota';
  sgSejarah.Cells[13,0]:='Kecamatan';
  sgSejarah.Cells[14,0]:='Desa';
  sgSejarah.Cells[15,0]:='RW';
  sgSejarah.Cells[16,0]:='RT';
  sgSejarah.Cells[17,0]:='Dusun';
  sgSejarah.Cells[18,0]:='Ket';
  sgSejarah.Cells[19,0]:='Status';
  sgSejarah.RowCount:=cRow;
  //TABEL-TABEL
  with ZTable1 do begin
    Active:=False;
    TableName:='penyimpan';
    Active:=True;
    SetLength(bPenyimpan,RecordCount);
    I:=0;
    while (not Eof) do begin
      bPenyimpan[I].ID:=FieldByName('ID').AsString;
      bPenyimpan[I].NAMA:=FieldByName('NAMA').AsString;
      Inc(I);
      Next;
    end;
  end;
  //desa
  with ZTable1 do begin
    Active:=False;
    TableName:='desa';
    Active:=True;
    SetLength(bDesa,RecordCount);
    I:=0;
    while (not Eof) do begin
      bDesa[I].KODE:=FieldByName('KODE_DESA').AsString;
      bDesa[I].NAMA:=FieldByName('TAMPIL').AsString;
      Inc(I);
      Next;
    end;
  end;
  //kec
  with ZTable1 do begin
    Active:=False;
    TableName:='kecamatan';
    Active:=True;
    SetLength(bKec,RecordCount);
    I:=0;
    while (not Eof) do begin
      bKec[I].KODE:=FieldByName('KODE_KECAMATAN').AsString;
      bKec[I].NAMA:=FieldByName('TAMPIL').AsString;
      Inc(I);
      Next;
    end;
  end;
  //kota
  with ZTable1 do begin
    Active:=False;
    TableName:='kabkot';
    Active:=True;
    SetLength(bKabkot,RecordCount);
    I:=0;
    while (not Eof) do begin
      bKabkot[I].KODE:=FieldByName('KODE').AsString;
      bKabkot[I].NAMA:=FieldByName('TAMPIL').AsString;
      Inc(I);
      Next;
    end;
    cbKabkot.Clear;
    cbKabkot.Items.Add('SEMUA KAB/KOTA');
    for I := 0 to Length(bKabkot) - 1 do
      begin
        cbKabkot.Items.Add(bKabkot[i].NAMA);
      end;
    cbKabkot.ItemIndex:=-1;
  end;
end;

procedure TForm_NView.cbDesaChange(Sender: TObject);
var
  desa:String;
begin
  if (cbdesa.ItemIndex > 0) then
  begin
    desa := cbdesa.Items[cbdesa.ItemIndex];
    //
    ZTable1.Active := False;
    ZTable1.TableName := 'desa';
    ZTable1.Filter := 'TAMPIL = '+QuotedStr(desa)+' AND KODE_KABKOT = '+QuotedStr(sKabkot);
    ZTable1.Filtered := True;
    ZTable1.Active := True;
    ZTable1.First;
    sDesa := ZTable1.FieldByName('KODE_DESA').AsString;
    btnRefresh.Enabled:=true;
    btnBuat.Enabled:=true;
    //buat
  end
    else
  begin
    sDesa:='';
    btnTambah.Enabled:=False;
    btnEdit.Enabled:=False;
    btnDaftar.Enabled:=false;
    btnHapus.Enabled:=False;
    btnRefresh.Enabled:=true;
  end;
end;

procedure TForm_NView.PageControl1Change(Sender: TObject);
var
  i:Integer;
begin
  lblket2.Caption:='';
  if (PageControl1.ActivePage=TabSheet1) then begin
    if (sgUtama.Row>0) then begin
      btnEdit.Enabled:=True;
      for i := 1 to sgGanda.RowCount - 1
        do sgGanda.Rows[i].Clear;
      sgGanda.RowCount:=2;
      if (cekBGanda(tid+'-'+pid)) then
         showGanda(tid+'-'+pid);
      txtHal2.Enabled:=false;
      btnNext2.Enabled:=false;
      btnPrev2.Enabled:=false;
    end;
  end
    else
  begin
    //history
    if (PageControl2.ActivePage=TabSheet3) then begin
      FetchDB2(1);
      btnKembalikan.Enabled:=False;
    end
      else
    if (PageControl2.ActivePage=TabSheet4) then begin
      FetchDB2(2);
      btnKembalikan.Enabled:=False;
    end
      else
    if (PageControl2.ActivePage=TabSheet5) then begin
      FetchDB2(3);
      btnKembalikan.Enabled:=False;
    end
      else
    if (PageControl2.ActivePage=TabSheet6) then begin
      FetchDB2(4);
      btnKembalikan.Enabled:=False;
    end;
  end;
end;

procedure TForm_NView.PageControl2Change(Sender: TObject);
begin
  if (PageControl2.ActivePage=TabSheet3) then begin
    FetchDB2(1);
    btnKembalikan.Enabled:=False;
  end
    else
  if (PageControl2.ActivePage=TabSheet4) then begin
    FetchDB2(2);
    btnKembalikan.Enabled:=False;
  end
    else
  if (PageControl2.ActivePage=TabSheet5) then begin
    FetchDB2(3);
    btnKembalikan.Enabled:=False;
  end
    else
  if (PageControl2.ActivePage=TabSheet6) then begin
    FetchDB2(4);
    btnKembalikan.Enabled:=False;
  end;
end;

procedure TForm_NView.FetchDB2(id:Integer);
var
  kode:String;
  query:String;
  kKota:String;
  kKec:String;
  kDesa:String;
  tblName:String;
  i:Integer;
  count:Integer;
  gdata:Boolean;
  que:String;
begin
  lblHal2.Caption:='-';
  gdata:=False;
  for i := 1 to sgsejarah.RowCount - 1
  do sgsejarah.Rows[i].Clear;
  sgsejarah.RowCount:=2;
  if (sDesa<>'') then
    begin
      kode:=sDesa;
    end
  else if (sKec<>'') then
    begin
      kode:=sKec;
    end
  else if (sKabkot<>'') then
    begin
      kode:=sKabkot;
    end
  else
    begin
      kode:='';
    end;
  if (kode='') then
    query:='1'
  else
    query:='NAMA LIKE "pemilih_'+kode+'%"';
  ZQuery1.Active := False;
  query:= 'SELECT * FROM penyimpan WHERE '+query;
  ZQuery1.SQL.Text := query;
  ZQuery1.Active := True;
  ZQuery1.First;  //dapat tabel yang di simpan
  SetLength(bTabel2,ZQuery1.RecordCount);
  i:=0;
  count:=0;
  while (not ZQuery1.Eof) do
    begin
      gdata:=True;
      ZQuery2.Active := False;
      tblName:=ZQuery1.FieldByName('NAMA').AsString;
      ZQuery2.SQL.Clear;
      que:='SELECT COUNT(*) FROM '+tblname+' WHERE ';
      Case id of
        1 : que:=que+'STATUS2='+QuotedStr('1');
        2 : que:=que+'STATUS2='+QuotedStr('2');
        3 : que:=que+'STATUS2='+QuotedStr('3');
        4 : que:=que+'STATUS2='+QuotedStr('4');
      else ShowMessage('Unknown');
      end;
//      showmessage('1 '+que);
      ZQuery2.SQL.Text:=que;
      ZQuery2.Active := True;
      ZQuery2.First;  //tabel
      count:=count+ZQuery2.Fields[0].AsInteger;
      //ShowMessage(IntToStr(count));
      if (ZQuery2.Fields[0].AsInteger>0) then begin
        bTabel2[i].ID:=ZQuery1.FieldByName('ID').AsString;
        bTabel2[i].TABEL:=ZQuery1.FieldByName('NAMA').AsString;
        bTabel2[i].COUNT:=count;
        Inc(i);
      end
        else
      SetLength(bTabel2,length(btabel2)-1);
      ZQuery1.Next;
    end;
    lblKet2.Caption:=IntToStr(count)+' data';
    jHal2:=count div bHal;
    if ((count mod bHal)>0) then jHal2:=jHal2+1;
    if (gdata and (count>0)) then TampilHalaman2(id,1);
end;

procedure TForm_NView.TampilHalaman2(id:Integer; Hal:Integer = 1);
var
  iter,i:Integer;
  tabel,iduser,tanda,query:String;
begin
  txtHal2.Enabled:=True;
  lblHal2.Caption := IntToStr(Hal)+' of '+IntToStr(jHal2);
  cHal2:=Hal;
  if (cHal2=1) then
    btnPrev2.Enabled:=False
  else
    btnPrev2.Enabled:=True;
  if (cHal2=jHal2) then
    btnNext2.Enabled:=False
  else
    btnNext2.Enabled:=True;
  //get to bpemilih
  GetData2(id,hal);
  cRow2:=1;
  while (cRow2<=bHal) and (crow2<=High(bpemilih2)) do
   begin
    sgSejarah.RowCount:=cRow2+1;
    tabel:=getTabel(bPemilih2[cRow2].ID);
    tabel:=getIdsimpan(tabel);
    iduser:=getIduser(bPemilih2[cRow2].ID);
    //
    sgSejarah.Cells[0,cRow2]:=IntToStr((cHal2-1)*bHal+cRow2);
    sgSejarah.Cells[1,cRow2]:=bPemilih2[cRow2].NIK;
    sgSejarah.Cells[2,cRow2]:=bPemilih2[cRow2].NKK;
    sgSejarah.Cells[3,cRow2]:=bPemilih2[cRow2].NAMA;
    sgSejarah.Cells[4,cRow2]:=bPemilih2[cRow2].TEMPAT;
    sgSejarah.Cells[5,cRow2]:=bPemilih2[cRow2].TANGGAL;
    sgSejarah.Cells[6,cRow2]:=bPemilih2[cRow2].IBU;
    sgSejarah.Cells[7,cRow2]:=bPemilih2[cRow2].AGAMA;
    sgSejarah.Cells[8,cRow2]:=bPemilih2[cRow2].KAWIN;
    sgSejarah.Cells[9,cRow2]:=bPemilih2[cRow2].KELAMIN;
    sgSejarah.Cells[10,cRow2]:=bPemilih2[cRow2].KELAINAN;
    sgSejarah.Cells[11,cRow2]:=bPemilih2[cRow2].ALAMAT;
    sgSejarah.Cells[12,cRow2]:=bPemilih2[cRow2].KOTA;
    sgSejarah.Cells[13,cRow2]:=bPemilih2[cRow2].KEC;
    sgSejarah.Cells[14,cRow2]:=bPemilih2[cRow2].DESA;
    sgSejarah.Cells[15,cRow2]:=bPemilih2[cRow2].RW;
    sgSejarah.Cells[16,cRow2]:=bPemilih2[cRow2].RT;
    sgSejarah.Cells[17,cRow2]:=bPemilih2[cRow2].DUSUN;
    sgSejarah.Cells[18,cRow2]:=bPemilih2[cRow2].KET;
    Case id of
      1 : sgSejarah.Cells[19,cRow2]:='Meninggal Dunia';
      2 : sgSejarah.Cells[19,cRow2]:='Data Fiktif';
      3 : sgSejarah.Cells[19,cRow2]:='TNI/POLRI';
      4 : sgSejarah.Cells[19,cRow2]:='Pindah';
    else ShowMessage('Unknown');
    end;
    Inc(crow2);
   end;
   //autosize
   for i := 0 to sgSejarah.ColCount - 1 do
    AutoSizeCol(sgSejarah, i);
end;

procedure TForm_NView.GetData2(id:Integer; Hal:Integer);
var
  query:String;
  kKota:String;
  kKec:String;
  kDesa:String;
  tabel:TTabel;
  tblName,idsimpan,iduser,idganda:String;
  i:Integer;
  vRow,ujung,awal:Integer;
begin
  dRow2:=1;
  vRow:=1;
  //cek
  ujung:=Hal*bHal;
  i:=0;
  if (jHal2>1) then begin
    for I := 0 to (Length(bTabel2)) - 1 do begin
      if (ujung<=bTabel2[i].COUNT) then
        break;
    end;
    if (i>0) then begin
      ujung:=ujung-btabel2[i-1].COUNT;
      if (ujung<bHal) then begin
        Dec(i);
        awal:=bTabel2[i].COUNT-(bHal-ujung);
      end else
        awal:=ujung-bHal;
    end
    else begin
      awal:=ujung-bHal;
    end;
  end
    else
  begin
    awal:=0;
    ujung:=bhal;
  end;
  //showmessage(IntToStr(length(btabel)));
  tblName:=btabel2[i].TABEL;
  idsimpan:=btabel2[i].ID;
  ZQuery1.Active := False;
  query:= 'SELECT * FROM '+tblName+' WHERE ';
  Case id of
    1 : query:=query+'STATUS2='+QuotedStr('1');
    2 : query:=query+'STATUS2='+QuotedStr('2');
    3 : query:=query+'STATUS2='+QuotedStr('3');
    4 : query:=query+'STATUS2='+QuotedStr('4');
  else ShowMessage('Unknown');
  end;
  query:=query+' ORDER BY NKK ASC LIMIT '+IntToStr(awal)+','+IntToStr(bhal);
//  showmessage('2 '+query);
  ZQuery1.SQL.Text := query;
  ZQuery1.Active := True;
  ZQuery1.First;  //tabel simpan
  kKota:=Copy(tblName,9,2);
  kKota:=getKabkot(kKota);
  kKec:=Copy(tblName,9,5);
  kKec:=getKec(kKec);
  kDesa:=Copy(tblName,9,8);
  kDesa:=getDesa(kDesa);
  dRow2:=dRow2+ZQuery1.RecordCount;
  SetLength(bPemilih2,dRow2);
  while (not ZQuery1.Eof) do begin
    bPemilih2[vRow].ID :=tblName+ZQuery1.Fields[0].AsString;
    bPemilih2[vRow].NIK:=ZQuery1.Fields[1].AsString;
    bPemilih2[vRow].NKK:=ZQuery1.Fields[2].AsString;
    bPemilih2[vRow].NAMA:=ZQuery1.Fields[3].AsString;
    bPemilih2[vRow].TEMPAT:=ZQuery1.Fields[4].AsString;
    bPemilih2[vRow].TANGGAL:=ZQuery1.Fields[5].AsString;
    bPemilih2[vRow].IBU:=ZQuery1.Fields[6].AsString;
    bPemilih2[vRow].AGAMA:=ZQuery1.Fields[7].AsString;
    bPemilih2[vRow].KAWIN:=ZQuery1.Fields[8].AsString;
    bPemilih2[vRow].KELAMIN:=ZQuery1.Fields[9].AsString;
    bPemilih2[vRow].KELAINAN:=ZQuery1.Fields[10].AsString;
    bPemilih2[vRow].ALAMAT:=ZQuery1.Fields[11].AsString;
    bPemilih2[vRow].KOTA:=kKota;
    bPemilih2[vRow].KEC:=kKec;
    bPemilih2[vRow].DESA:=kDesa;
    bPemilih2[vRow].RW:=ZQuery1.Fields[12].AsString;
    bPemilih2[vRow].RT:=ZQuery1.Fields[13].AsString;
    bPemilih2[vRow].DUSUN:=ZQuery1.Fields[14].AsString;
    bPemilih2[vRow].KET:=ZQuery1.Fields[15].AsString;
    Inc(vrow);
    ZQuery1.Next;
  end;
  if ((ujung<bhal) and (jhal2>1)) then begin
    inc(i);
    tblName:=btabel2[i].TABEL;
    idsimpan:=btabel2[i].ID;
    ZQuery1.Active := False;
    query:= 'SELECT * FROM '+tblName+' WHERE ';
    Case id of
      1 : query:=query+'STATUS2='+QuotedStr('1');
      2 : query:=query+'STATUS2='+QuotedStr('2');
      3 : query:=query+'STATUS2='+QuotedStr('3');
      4 : query:=query+'STATUS2='+QuotedStr('4');
    else ShowMessage('Unknown');
    end;
    query:=query+' ORDER BY NKK ASC LIMIT 0,'+IntToStr(ujung);
//    showmessage('3 '+query);
    ZQuery1.SQL.Text := query;
    ZQuery1.Active := True;
    ZQuery1.First;  //tabel simpan
    kKota:=Copy(tblName,9,2);
    kKota:=getKabkot(kKota);
    kKec:=Copy(tblName,9,5);
    kKec:=getKec(kKec);
    kDesa:=Copy(tblName,9,8);
    kDesa:=getDesa(kDesa);
    dRow2:=dRow2+ZQuery1.RecordCount;
    SetLength(bPemilih2,dRow2);
    while (not ZQuery1.Eof) do begin
      bPemilih2[vRow].ID :=tblName+ZQuery1.Fields[0].AsString;
      bPemilih2[vRow].NIK:=ZQuery1.Fields[1].AsString;
      bPemilih2[vRow].NKK:=ZQuery1.Fields[2].AsString;
      bPemilih2[vRow].NAMA:=ZQuery1.Fields[3].AsString;
      bPemilih2[vRow].TEMPAT:=ZQuery1.Fields[4].AsString;
      bPemilih2[vRow].TANGGAL:=ZQuery1.Fields[5].AsString;
      bPemilih2[vRow].IBU:=ZQuery1.Fields[6].AsString;
      bPemilih2[vRow].AGAMA:=ZQuery1.Fields[7].AsString;
      bPemilih2[vRow].KAWIN:=ZQuery1.Fields[8].AsString;
      bPemilih2[vRow].KELAMIN:=ZQuery1.Fields[9].AsString;
      bPemilih2[vRow].KELAINAN:=ZQuery1.Fields[10].AsString;
      bPemilih2[vRow].ALAMAT:=ZQuery1.Fields[11].AsString;
      bPemilih2[vRow].KOTA:=kKota;
      bPemilih2[vRow].KEC:=kKec;
      bPemilih2[vRow].DESA:=kDesa;
      bPemilih2[vRow].RW:=ZQuery1.Fields[12].AsString;
      bPemilih2[vRow].RT:=ZQuery1.Fields[13].AsString;
      bPemilih2[vRow].DUSUN:=ZQuery1.Fields[14].AsString;
      bPemilih2[vRow].KET:=ZQuery1.Fields[15].AsString;
      Inc(vrow);
      ZQuery1.Next;
    end;
  end;
end;

procedure TForm_NView.sgGandaClick(Sender: TObject);
var
  pos,tots,i:Integer;
begin
  if (sgGanda.Row>0) then begin
      btnKembalikan.Enabled:=True;
      pos:=sgGanda.Row;
      //getID
      ptable2:=bgridganda[pos].nama;
      pid2:=bgridganda[pos].id;
  end;
end;

procedure TForm_NView.sgSejarahClick(Sender: TObject);
var
  pos,tots,i:Integer;
begin
  if (sgSejarah.Row>0) and (sgSejarah.Cells[1,1]<>'') then begin
      pos:=sgSejarah.Row;
      //getID
      ptable2:=Copy(bPemilih2[pos].ID,1,16);
      tots:=Length(bPemilih2[pos].ID)-16;
      pid2:=Copy(bPemilih2[pos].ID,17,tots);
      tid2:=getIdsimpan(ptable2);
      //
      btnKembalikan.Enabled:=True;
  end;
end;

procedure TForm_NView.sgUtamaClick(Sender: TObject);
var
  pos,tots,i:Integer;
begin
  if (sgUtama.Row>0) and (sgUtama.Cells[1,1]<>'') then begin
      btnEdit.Enabled:=True;
      btnHapus.Enabled:=true;
      pos:=sgUtama.Row;
      //getID
      ptable:=Copy(bPemilih[pos].ID,1,16);
      tots:=Length(bPemilih[pos].ID)-16;
      pid:=Copy(bPemilih[pos].ID,17,tots);
      tid:=getIdsimpan(ptable);
      //
      btnKembalikan.Enabled:=false;
      if (PageControl1.ActivePage=TabSheet1) then begin
        //data ganda
        for i := 1 to sgGanda.RowCount - 1
          do sgGanda.Rows[i].Clear;
        sgGanda.RowCount:=2;
        if (cekBGanda(tid+'-'+pid)) then
           showGanda(tid+'-'+pid);
        txtHal2.Enabled:=false;
        btnNext2.Enabled:=false;
        btnPrev2.Enabled:=false;
      end;
  end;
end;

function TForm_NView.cekBGanda(id:String):Boolean;
var
  res:Boolean;
  temp:TGanda;
begin
  res:=false;
  for temp in bGanda do begin
    if (temp.ID1=id) then begin
      res:=True;
      break;
    end;
  end;
  Result:=res;
end;

procedure TForm_NView.showGanda(idganda:String);
var
  i,srow:Integer;
  tabel,tid,pid,id2,id1:String;
  idkabkot,idkec,idesa:String;
  temp:TStrings;
begin
  srow:=2;
  SetLength(bGridganda,srow);
  for I := 0 to Length(bGanda) - 1 do begin
    if (bGanda[i].ID1=idganda) then begin
      id2:=bganda[i].ID2;
      temp := TStringList.Create;
      try
        ExtractStrings(['-'], [], PChar(id2), temp);
        tid := temp[0];
        pid := temp[1];
      finally
        temp.Free;
      end;

      tabel:=getSimpan(tid);
      idkabkot:=Copy(tabel,9,2);
      idkec:=Copy(tabel,9,5);
      idesa:=Copy(tabel,9,8);
      ZTable2.Active:=false;
      ZTable2.TableName:=tabel;
      ZTable2.Filter:='ID_USER='+QuotedStr(pid);
      ZTable2.Filtered:=true;
      ZTable2.active:=true;
      ZTable2.First;
        sgGanda.Cells[0,srow-1]:=IntToStr(sRow-1);
        sgGanda.Cells[1,srow-1]:=ZTable2.FieldByName('NIK').AsString;
        sgGanda.Cells[2,srow-1]:=ZTable2.FieldByName('NKK').AsString;
        sgGanda.Cells[3,srow-1]:=ZTable2.FieldByName('NAMA').AsString;
        sgGanda.Cells[4,srow-1]:=ZTable2.FieldByName('TEMPAT').AsString;
        sgGanda.Cells[5,srow-1]:=ZTable2.FieldByName('TANGGAL').AsString;
        sgGanda.Cells[6,srow-1]:=ZTable2.FieldByName('IBU').AsString;
        sgGanda.Cells[7,srow-1]:=ZTable2.FieldByName('AGAMA').AsString;
        sgGanda.Cells[8,srow-1]:=ZTable2.FieldByName('KAWIN').AsString;
        sgGanda.Cells[9,srow-1]:=ZTable2.FieldByName('KELAMIN').AsString;
        sgGanda.Cells[10,srow-1]:=ZTable2.FieldByName('KELAINAN').AsString;
        sgGanda.Cells[11,srow-1]:=ZTable2.FieldByName('ALAMAT').AsString;
        sgGanda.Cells[12,srow-1]:=getKabkot(idkabkot);
        sgGanda.Cells[13,srow-1]:=getKec(idkec);
        sgGanda.Cells[14,srow-1]:=getDesa(idesa);
        sgGanda.Cells[15,srow-1]:=ZTable2.FieldByName('RW').AsString;
        sgGanda.Cells[16,srow-1]:=ZTable2.FieldByName('RT').AsString;
        sgGanda.Cells[17,srow-1]:=ZTable2.FieldByName('DUSUN').AsString;
        sgGanda.Cells[18,srow-1]:=ZTable2.FieldByName('KET').AsString;
        bGridganda[srow-1].ID:=pid;
        bGridganda[srow-1].NAMA:=tabel;
      Inc(srow);
      sgGanda.RowCount:=srow;
    end;
  end;
  lblKet2.Caption:=IntToStr(srow)+' data';
  for i := 0 to sgGanda.ColCount - 1 do
      AutoSizeCol(sgGanda, i);
end;

procedure TForm_NView.txtHalKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if key = VK_RETURN then
    getPage;
end;

procedure TForm_NView.getPage();
begin
    if ((txtHal.Text='') or (StrToInt(txtHal.Text)>jHal) or (StrToInt(txtHal.Text)<1)) then
      ShowMessage('Isi dahulu dengan halaman yang sesuai')
    else if (jHal=0) then
      ShowMessage('Tidak ada halaman yang dapat ditampilkan')
    else
      begin
        TampilHalaman(StrToInt(txtHal.Text));
      end;
end;

end.
